# Maintainer: Hiroshi Hatake <cosmo0920.wp@gmail.com>

_realname=groonga
pkgbase=mingw-w64-${_realname}
pkgname="${MINGW_PACKAGE_PREFIX}-${_realname}"
pkgver=15.0.9
pkgrel=1
pkgdesc="An opensource fulltext search engine (mingw-w64)"
arch=('any')
mingw_arch=('mingw64' 'ucrt64' 'clang64' 'clangarm64')
url="https://groonga.org/"
license=('spdx:LGPL-2.1-or-later')
source=(https://packages.groonga.org/source/${_realname}/${_realname}-${pkgver}.tar.gz{,.asc}
        001-aarch64.patch
        002-llama_cpp-add-stdint.patch)
depends=("${MINGW_PACKAGE_PREFIX}-arrow"
         "${MINGW_PACKAGE_PREFIX}-blosc2"
         "${MINGW_PACKAGE_PREFIX}-lz4"
         "${MINGW_PACKAGE_PREFIX}-mecab"
         "${MINGW_PACKAGE_PREFIX}-mecab-naist-jdic"
         "${MINGW_PACKAGE_PREFIX}-msgpack-c"
         "${MINGW_PACKAGE_PREFIX}-onigmo"
         "${MINGW_PACKAGE_PREFIX}-xxhash"
         "${MINGW_PACKAGE_PREFIX}-zlib"
         "${MINGW_PACKAGE_PREFIX}-zstd")
makedepends=("git"
             "${MINGW_PACKAGE_PREFIX}-cc"
             "${MINGW_PACKAGE_PREFIX}-cmake"
             "${MINGW_PACKAGE_PREFIX}-ninja"
             "${MINGW_PACKAGE_PREFIX}-rapidjson"
             "${MINGW_PACKAGE_PREFIX}-ruby"
             "${MINGW_PACKAGE_PREFIX}-xsimd")
sha256sums=('f9969d2f8025a31dd201d8ce0e5db3a1547144e6191f75a9bbb1d771b7f0def2'
            'SKIP'
            '5fbb336487a6522e2f7bff01ea3cdd714d4c27de3cc65d5a6ecb2eedeb6fc2c4'
            '823f83e2043fc4b98c5971a12a65c1c4cb472b49b0e812052746aa591b7cdf44')
validpgpkeys=('2701F317CFCCCB975CADE9C2624CF77434839225') # Groonga Key <packages@groonga.org>

apply_patch_with_msg() {
  for _patch in "$@"
  do
    msg2 "Applying ${_patch}"
    patch -Nbp1 -i "${srcdir}/${_patch}"
  done
}

prepare() {
  cd "${_realname}-${pkgver}"

  apply_patch_with_msg \
    001-aarch64.patch
}

build() {
  mkdir -p "${srcdir}"/build-${MSYSTEM} && cd "${srcdir}"/build-${MSYSTEM}

  declare -a _extra_config
  if check_option "debug" "n"; then
    _extra_config+=("-DCMAKE_BUILD_TYPE=Release")
  else
    _extra_config+=("-DCMAKE_BUILD_TYPE=Debug")
  fi

  CFLAGS+=" -Wno-incompatible-pointer-types" \
  MSYS2_ARG_CONV_EXCL="-DCMAKE_INSTALL_PREFIX=" \
    ${MINGW_PREFIX}/bin/cmake.exe \
      -G Ninja \
      "${_extra_config[@]}" \
      -DCMAKE_INSTALL_PREFIX=${MINGW_PREFIX} \
      -DGRN_WITH_APACHE_ARROW=ON \
      -DGRN_WITH_BLOSC=auto \
      -DGRN_WITH_MRUBY=ON \
      ../${_realname}-${pkgver}

  ${MINGW_PREFIX}/bin/cmake --build . || true

  # workaround: gcc-15 compiler error
  (
    cd _deps/llama_cpp-src
    patch -p1 -i "${srcdir}"/002-llama_cpp-add-stdint.patch
  )

  ${MINGW_PREFIX}/bin/cmake --build .
}

package() {
  cd "${srcdir}"/build-${MSYSTEM}
  DESTDIR="${pkgdir}" ${MINGW_PREFIX}/bin/cmake --install .

  local PREFIX_DEPS=$(cygpath -am ${MINGW_PREFIX})
  pushd "${pkgdir}${MINGW_PREFIX}/lib/pkgconfig"
  for pc in *.pc; do
    sed -s "s|${PREFIX_DEPS}|${MINGW_PREFIX}|g" -i ${pc}
  done
  popd

  install -Dm644 \
    "${srcdir}"/${_realname}-${pkgver}/COPYING \
    "${pkgdir}${MINGW_PREFIX}/share/licenses/${_realname}/COPYING"
}
